<?php
namespace Common\Logic\Admin;

use Common\Base\LogicBase;
use Common\Model\Admin\AdminModel;

class AdminLogic extends LogicBase
{
	public $m_admin;

	public function __construct()
	{
		$this->m_admin = new AdminModel();
	}

	/**
     * 得到用户列表，关联group
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getAdminList($w=3, $search=array(), $fields='a.*,b.group_name', $page=0, $pageSize=0, $by='', $exclude='')
    {
        return $this->m_admin->getAdminList($w, $search, $fields, $page, $pageSize, $by, $exclude);
    }

    /**
     * 得到用户列表
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getTableAdmin($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='', $exclude='')
    {
        return $this->m_admin->getTableAdmin($w, $search, $fields, $page, $pageSize, $by, $exclude);
    }

    /**
     * 添加用户
     *
     * @param $post
     *
     * @return array
     */
    public function addAdmin($post = array())
    {
        $row = array();

        if(isset($post['admin_group_id']) && intval($post['admin_group_id'])>0){
            $row['admin_group_id'] = intval($post['admin_group_id']);
        }else{
            $this->setError('请选择分组');
            return false;
        }

        if(isset($post['admin_name']) && $post['admin_name']!=''){
            //检查用户名是否已经存在
            $ck = $this->getAdminList(1, array('admin_name'=>$post['admin_name']));
            if($ck > 0){ $this->setError('用户名已经存在'); return false; }
            $row['admin_name'] = $post['admin_name'];
        }else{
            $this->setError('请输入用户名');
            return false;
        }

        if(isset($post['admin_password']) && $post['admin_password']!=''){
            $row['admin_password'] = mj_encode($post['admin_password']);
        }else{
            $this->setError('请输入密码');
            return false;
        }

        if(is_array($post['auth']) && count($post['auth'])){
            $auth = array();
            foreach($post['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $auth[] = $v;
                }
            }
            if(count($auth)){
                sort($auth);
                $row['admin_acl'] = implode(',', $auth);
            }else{
                $row['admin_acl'] = '';
            }
        }else{
            $row['admin_acl'] = '';
        }

        $row['ctime'] = time();
        $row['st'] = 1;

        $insertID = $this->m_admin->addAdmin($row);
        if(false === $insertID){
            $this->setError($this->m_admin->getError());
            return false;
        }else{
            return $insertID;
        }
    }

    /**
     * 修改用户
     *
     * @param $post
     *
     * @return array
     */
    public function editAdmin($post = array())
    {
        if(isset($post['admin_id']) && intval($post['admin_id'])>1){
            $admin_id = intval($post['admin_id']);
        }else{
            $this->setError('参数错误');
            return false;
        }

        $row = array();

        if(isset($post['admin_group_id']) && intval($post['admin_group_id'])>0){
            $row['admin_group_id'] = intval($post['admin_group_id']);
        }

        if(isset($post['admin_password']) && $post['admin_password']!=''){
            $row['admin_password'] = mj_encode($post['admin_password']);
        }

        if(is_array($post['auth']) && count($post['auth'])){
            $auth = array();
            foreach($post['auth'] as $v){
                $v = intval($v);
                if($v > 0){
                    $auth[] = $v;
                }
            }
            if(count($auth)){
                sort($auth);
                $row['admin_acl'] = implode(',', $auth);
            }else{
                $row['admin_acl'] = '';
            }
        }else{
            $row['admin_acl'] = '';
        }

        $aff = $this->updateAdminByAdminId($row, $admin_id);
        if(false === $aff){
            $this->setError($this->m_admin->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 修改用户
     *
     * @param array $dat 需要更新的数据
     * @param int   $admin_id
     *
     * @return array
     */
    public function updateAdminByAdminId($dat=array(), $admin_id=0)
    {
        $aff = $this->m_admin->updateAdminByAdminId($dat, $admin_id);
        if(false === $aff){
            $this->setError($this->m_admin->getError());
            return false;
        }else{
            return $aff;
        }
    }

    /**
     * 更改密码
     *
     * @param array $post
     * @param int   $admin_id
     *
     * @return array
     */
    public function changePassword($post=array(), $admin_id=0)
    {
        $admin_id = intval($admin_id);
        if($admin_id < 1){
            $this->setError('缺少admin_id');
            return false;
        }

        if(isset($post['formPassword']) && $post['formPassword']!=''){
            $formPassword = $post['formPassword'];
        }else{
            $this->setError('请输入原密码');
            return false;
        }

        if(isset($post['admin_password']) && $post['admin_password']!=''){
            $admin_password = $post['admin_password'];
        }else{
            $this->setError('请输入新密码');
            return false;
        }

        if(isset($post['rpPassword']) && $post['rpPassword']!=''){
            $rpPassword = $post['rpPassword'];
        }else{
            $this->setError('请再次输入新密码');
            return false;
        }

        if($rpPassword != $admin_password){ $this->setError('两次输入的新密码不同'); return false; }

        //检查原密码是否正确
        $ck = $this->getTableAdmin(4, array('admin_id'=>$admin_id), 'admin_password');
        if(empty($ck)){
            $this->setError('无此用户');
            return false;
        }else{
            if($ck['admin_password'] != mj_encode($formPassword)){ $this->setError('pwdErr'); return false; }
            else{ return $this->updateAdminByAdminId(array('admin_password'=>mj_encode($admin_password)), $admin_id); }
        }
    }


}